package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalDateTime;

/**
 * 用户实体类 - 数据库表映射
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString(exclude = {"classEntity", "password"})
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "users", indexes = {
        @Index(name = "idx_user_username", columnList = "username"),
        @Index(name = "idx_user_role", columnList = "role"),
        @Index(name = "idx_user_class", columnList = "class_id"),
        @Index(name = "idx_user_created", columnList = "create_time")
})
public class UserEntity {

    /**
     * 用户ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 用户名（唯一）
     */
    @NotBlank(message = "用户名不能为空")
    @Size(min = 3, max = 50, message = "用户名长度必须在3-50个字符之间")
    @Column(name = "username", unique = true, nullable = false, length = 50)
    private String username;

    /**
     * 用户姓名
     */
    @NotBlank(message = "姓名不能为空")
    @Size(max = 100, message = "姓名长度不能超过100个字符")
    @Column(name = "name", nullable = false, length = 100)
    private String name;

    /**
     * 用户角色
     */
    @NotBlank(message = "用户角色不能为空")
    @Column(name = "role", nullable = false, length = 20)
    private String role;

    /**
     * 密码（加密存储）
     */
    @NotBlank(message = "密码不能为空")
    @Size(min = 6, message = "密码长度至少6位")
    @Column(name = "password", nullable = false, length = 255)
    private String password;

    /**
     * 学号
     */
    @Size(max = 20, message = "学号长度不能超过20个字符")
    @Column(name = "student_id", length = 20)
    private String studentId;

    /**
     * 教师工号
     */
    @Size(max = 20, message = "教师工号长度不能超过20个字符")
    @Column(name = "teacher_id", length = 20)
    private String teacherId;

    /**
     * 手机号
     */
    @Size(max = 15, message = "手机号长度不能超过15个字符")
    @Column(name = "phone", length = 15)
    private String phone;

    /**
     * 邮箱
     */
    @Email(message = "邮箱格式不正确")
    @Size(max = 100, message = "邮箱长度不能超过100个字符")
    @Column(name = "email", length = 100)
    private String email;

    /**
     * 头像URL
     */
    @Size(max = 500, message = "头像URL长度不能超过500个字符")
    @Column(name = "avatar", length = 500)
    private String avatar;

    /**
     * 所属班级
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "class_id")
    private ClassEntity classEntity;

    /**
     * 学院
     */
    @Size(max = 100, message = "学院名称长度不能超过100个字符")
    @Column(name = "college", length = 100)
    private String college;

    /**
     * 专业
     */
    @Size(max = 100, message = "专业名称长度不能超过100个字符")
    @Column(name = "specialty", length = 100)
    private String specialty;
    // === 便捷方法 ===

    /**
     * 获取班级ID
     */
    public Long getClassId() {
        return classEntity != null ? classEntity.getClassId() : null;
    }

    /**
     * 获取班级名称
     */
    public String getClassName() {
        return classEntity != null ? classEntity.getClassName() : null;
    }

    /**
     * 获取年级
     */
    public String getGradeLevel() {
        return classEntity != null && classEntity.getGrade() != null
                ? classEntity.getGrade().getGradeName() : null;
    }

    /**
     * 验证用户是否有效
     */
    public boolean isValid() {
        return username != null && !username.trim().isEmpty() &&
                name != null && !name.trim().isEmpty() &&
                role != null && !role.trim().isEmpty() &&
                password != null && !password.trim().isEmpty();
    }

    /**
     * 是否是教师
     */
    public boolean isTeacher() {
        return "TEACHER".equalsIgnoreCase(role) || "ADMIN".equalsIgnoreCase(role);
    }

    /**
     * 是否是学生
     */
    public boolean isStudent() {
        return "STUDENT".equalsIgnoreCase(role);
    }

    /**
     * 是否是管理员
     */
    public boolean isAdmin() {
        return "ADMIN".equalsIgnoreCase(role);
    }
}
